@extends('admin.layout')
@section('title', $title)
@section('content')
    <div class="content-wrapper" style="min-height: 946px;">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                商品管理
                <small>商品编辑</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
                <li><a href="#">商品管理</a></li>
                <li class="active">商品编辑</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <!-- left column -->
                <div class="col-md-12">
                    <!-- general form elements -->
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h6 class="btn btn-primary">商品信息</h6>
                            <h6 class="btn">商品描述</h6>
                            <h6 class="btn">商品类型</h6>
                            <h6 class="btn">商品相册</h6>
                        </div><!-- /.box-header -->
                        <!-- form start -->
                     @if (count($errors) > 0)
                      <div id="jg" class="callout callout-info">
                        <h4>警告</h4>
                        @foreach ($errors->all() as $error)
                        <p>{{ $error }}</p>
                        @endforeach
                      </div>
                       @endif
                        @if(session('info'))
                        <div id="jg" class="callout callout-info">
                        <h4>{{ session('info') }}</h4> 
                        </div>   
                        @endif 
                        <form role="form" action="{{ url('/admin/goods/update') }}/{{ $data -> id }}" method="post" enctype="multipart/form-data">
                            {{ csrf_field()}}
                            <div class="box-body">
                                 <div class="form-group">
                                    <label for="exampleInputEmail1">主分类</label>
                                    <select class="form-control" name="cat_id">
                                    @foreach($category as $v)
                                        <option value="{{ $v -> id }}"
                                            @if($v -> id == $data -> cat_id)
                                                selected
                                            @endif
                                        >{{ $v -> name }}</option>
                                    @endforeach
                                    </select>
                                 </div>
                                 @if($goods_cat)
                                 @foreach($goods_cat as $v1)
                                 <div class="form-group" id="kuozhan">
                                    <label for="exampleInputEmail1">扩展分类<a onclick="addKuozhan(this)" href="#">[+]</a></label>
                                    <select class="form-control" name="ext_cat_id[]">
                                    <option value="">请选择</option>
                                    @foreach($category as $v)
                                        <option value="{{ $v -> id }}"
                                        @if($v -> id == $v1 -> cat_id)
                                        selected="selected"
                                        @endif
                                        >{{ $v -> name }}</option>
                                    @endforeach
                                    </select>
                                 </div>                                               @endforeach
                                 @else
                                     <div class="form-group" id="kuozhan">
                                    <label for="exampleInputEmail1">扩展分类<a onclick="addKuozhan(this)" href="#">[+]</a></label>
                                    <select class="form-control" name="ext_cat_id[]">
                                    <option value="">请选择</option>
                                    @foreach($category as $v)
                                        <option value="{{ $v -> id }}">{{ $v -> name }}</option>
                                    @endforeach
                                    </select>
                                 </div>                                
                                 @endif               
                                <div class="form-group" id="goods_name">
                                    <label for="exampleInputEmail1">商品名称</label>
                                    <input type="text" name="goods_name" class="form-control" id="exampleInputEmail1" placeholder="商品名称" value="{{ $data -> goods_name }}">
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputPassword1">商品价格</label>
                                    <input type="text"  name="goods_price" class="form-control" id="exampleInputPassword1" placeholder="商品价格" value=" {{$data -> goods_price }} ">
                                </div>
                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-md-4">
                                            <label for="exampleInputPassword1">促销价格</label>
                                            <input type="text" name="promote_price" class="form-control" id="exampleInputPassword1" placeholder="促销价格"                                                                                  @if($data -> promote_price == 0)
                                                value="0"
                                                @else
                                                 value="{{ $data -> promote_price }}"
                                                @endif
                                                >
                                        </div>
                                        <div class="col-md-4">
                                            <label for="exampleInputPassword1">促销开始时间</label>
                                            <input  onClick="WdatePicker()" type="text" name="promote_start_time"  class="form-control" id="exampleInputPassword1" placeholder="促销开始时间"
                                            @if($data -> promote_start_time == 0)
                                            value = ''
                                            @else
                                            value = "{{ date('Y-m-d H:i:s',  $data -> promote_start_time) }}"
                                            @endif
                                            >
                                        </div>
                                        <div class="col-md-4">
                                            <label for="exampleInputPassword1">促销结束时间</label>
                                            <input onClick="WdatePicker()" type="text" name="promote_end_time" class="form-control" id="exampleInputPassword1" placeholder="促销结束时间"
                                            @if($data -> promote_end_time == 0)
                                            value = ''
                                            @else
                                            value = "{{ date('Y-m-d H:i:s',  $data -> promote_end_time) }}"
                                            @endif
                                            >
                                        </div>                                      
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="">
                                        <label for="exampleInputPassword1">排序数字</label>
                                        <input type="text" name="sort_num" class="form-control" id="exampleInputPassword1" placeholder="排序数字 默认100" value="{{ $data -> sort_num }}">
                                    </div>                        
                                </div>
                                <div class="form-group">
                                    <div class="">
                                        <label for="exampleInputPassword1">系列类型</label>
                                        <input type="text" name="series" class="form-control" id="exampleInputPassword1" placeholder="排序数字 默认100" value="{{ $data -> series }}">
                                    </div>                        
                                </div>
                                <div class="form-group">
                                    <div class="">
                                        <label for="exampleInputPassword1">商品特点</label>
                                        <input type="text" name="goods_point" class="form-control" value="{{ $data -> goods_point}}" id="exampleInputPassword1" placeholder="请填写商品特点">
                                    </div>                        
                                </div>
                                <div class="form-group">
                                    <div class="">
                                        <label for="exampleInputPassword1">商品提示</label>
                                        <input type="text" name="goods_attention" class="form-control" value="{{ $data -> goods_attention }}" id="exampleInputPassword1" placeholder="请填写商品提示">
                                    </div>                        
                                </div>                                      
                                <div class="form-group">
                                    <img src="@if($data -> logo != '')
                                        {{ url('/uploads/goods_pic')}}/{{$data -> logo}}
                                    @endif" alt="商品图片" width="100" height="100"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="logo" id="exampleInputFile">
                                    <p class="help-block">漂亮的商品图片</p>
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputFile">是否新品</label>
                                    <div class="radio">
                                        <label>
                                            <input type="radio" name="is_new" id="optionsRadios1" value="1"
                                            @if($data -> is_new == 1)
                                            checked
                                            @endif
                                            >是
                                        </label>
                                        <label>
                                            <input type="radio" name="is_new" id="optionsRadios1" value="0"
                                            @if($data -> is_new == 0)
                                            checked
                                            @endif                                            
                                            >否
                                        </label>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputFile">是否热卖</label>
                                    <div class="radio">
                                        <label>
                                            <input type="radio" name="is_hot" id="optionsRadios1" value="1"
                                            @if($data -> is_hot== 1)
                                            checked
                                            @endif
                                            >是
                                        </label>
                                        <label>
                                            <input type="radio" name="is_hot" id="optionsRadios1" value="0"
                                             @if($data -> is_hot == 0)
                                            checked
                                            @endif 
                                            >否
                                        </label>
                                    </div>
                                </div>                               
                            </div><!-- /.box-body -->
                            <!-- 商品描述 -->
                            <div class="box-body" style="display:none">
                              <!-- 加载编辑器的容器 -->
                                <script id="container" name="goods_desc" type="text/plain" style="width:100%">{!! $data -> goods_desc !!}</script>
                                <!-- 配置文件 -->
                                <script type="text/javascript" src="{{ asset('/ue/ueditor.config.js') }}"></script>
                                <!-- 编辑器源码文件 -->
                                <script type="text/javascript" src="{{ asset('/ue/ueditor.all.js') }}"></script>
                                <!-- 实例化编辑器 -->
                                <script type="text/javascript">
                                    var ue = UE.getEditor('container');
                                </script>                               
                             </div>
                             <!-- 商品类型 -->
                             <div class="box-body" style="display:none">
                                <div class="form-group">
                                    <label for="exampleInputEmail1">商品类型</label>
                                    <select class="form-control" name="type_id">
                                        <option value="">请选择</option>
                                           @foreach($type as $v)
                                            <option value="{{ $v -> id }}"
                                            @if($data -> type_id == $v -> id)
                                            selected
                                            @endif 
                                            >{{ $v -> type_name }}</option>
                                           @endforeach
                                    </select>
                                     <div class="form-group">
                                        <ul style="list-style:none">
                                            <?php  $a =array() ?>
                                            @foreach($attribute as $v)
                                           <?php 
                                           if(in_array($v -> id,$a))
                                            {
                                                $opt = '-';
                                            }else
                                            {
                                                $opt = '+';
                                                $a[] = $v -> id;
                                            }
                                           ?>
                                            <li>
                                            <!-- 做一个商品属性的隐藏域 判断是原有属性还是新加属性 -->
                                                <input type="hidden" name="goods_attr_id[]" value="{{ $v -> goods_attr_id }}">
                                                @if($v -> attr_type == 1)
                                                <a onclick="addAttr(this)" href="#"></a>
                                                @endif
                                                {{ $v -> attr_name}} :
                                                @if($v -> attr_option_values)
                                                <select class="form-control" name="attr_value[{{ $v -> id }}][]">
                                                    <option value="">请选择</option>
                                                    <?php $arr = explode(',',$v -> attr_option_values) ?>
                                                    @foreach($arr as $v1)
                                                    <option value="{{ $v1 }}"
                                                    @if($v1 == $v -> attr_value)
                                                        selected
                                                    @endif
                                                    >
                                                       <?php echo $v1 ?>
                                                    </option>
                                                    @endforeach
                                                </select>
                                                @else
                                                <input  name="attr_value[{{ $v -> id }}][]" type="text" value="{{ $v -> attr_value }}">
                                                @endif
                                            </li>
                                            @endforeach
                                        </ul>
                                     </div>
                                 </div>

                                <div class="form-group">
                                <ul class="ul" style="list-style:none"></ul>
                                </div>
                             </div>
                            <div class="box-body" style="display:none">
                                <div class="form-group">
                                    <img src="@if($data -> one_pic != '')
                                        {{ url('/uploads/goods_pic')}}/{{$data -> one_pic}}
                                    @endif" alt="商品图片" width="100" height="100"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="one_pic" id="exampleInputFile">
                                    <p class="help-block">详情图1</p>
                                </div>
                                <div class="form-group">
                                    <img src="@if($data -> two_pic != '')
                                        {{ url('/uploads/goods_pic')}}/{{$data -> two_pic}}
                                    @endif" alt="商品图片" width="100" height="100"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="two_pic" id="exampleInputFile">
                                    <p class="help-block">详情图2</p>
                                </div>
                                <div class="form-group">
                                    <img src="@if($data -> thr_pic != '')
                                        {{ url('/uploads/goods_pic')}}/{{$data -> thr_pic}}
                                    @endif" alt="商品图片" width="100" height="100"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="thr_pic" id="exampleInputFile">
                                    <p class="help-block">详情图3</p>
                                </div>
                                <div class="form-group">
                                    <img src="@if($data -> fou_pic != '')
                                        {{ url('/uploads/goods_pic')}}/{{$data -> fou_pic}}
                                    @endif" alt="商品图片" width="100" height="100"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="fou_pic" id="exampleInputFile">
                                    <p class="help-block">详情图4</p>
                                </div>
                                <div class="form-group">
                                    <img src="@if($data -> fiv_pic != '')
                                        {{ url('/uploads/goods_pic')}}/{{$data -> fiv_pic}}
                                    @endif" alt="商品图片" width="100" height="100"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="fiv_pic" id="exampleInputFile">
                                    <p class="help-block">详情图5</p>
                                </div>
                                <div class="form-group">
                                    <img src="@if($data -> six_pic != '')
                                        {{ url('/uploads/goods_pic')}}/{{$data -> six_pic}}
                                    @endif" alt="商品图片" width="100" height="100"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="six_pic" id="exampleInputFile">
                                    <p class="help-block">详情图6</p>
                                </div>                                    
                             </div>
                            <div class="box-footer">
                                <button type="submit" class="btn btn-primary">提交</button>
                                 <button type="reset" class="btn btn-primary">重置</button>
                            </div>
                        </form>
                    </div><!-- /.box -->
                </div><!--/.col (left) -->
                <!-- right column -->
            </div>   <!-- /.row -->
        </section><!-- /.content -->
    </div>
 <!-- 切换的代码 -->
 <script type="text/javascript">
    window.onload=function()
    {
        setInterval(function(){
            $('#jg').slideUp();
        },3000);
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $('h6').on('click',function(){
            //获取点击的第i个按钮
            var i =$(this).index();
            //隐藏所有的
            $('.box-body').hide();
            //显示第i个box-body
            $('.box-body').eq(i).show();    
            $(this).addClass('btn-primary');
            $(this).siblings().removeClass('btn-primary');
        });

        $('select[name=type_id]').on('change',function(){
            //获取所选下拉框的id
            var type_id = $(this).val();
            //根据id使用ajax 返回该类型下的属性

            if(type_id != null)
            {
                $.ajax({
                    type:"POST",
                    url:"{{ url('admin/goods/ajaxAttribute') }}",
                    data:{type_id:type_id},
                    dataType:'json',
                    success:function(data)
                    {
                        //返回的为json数据 为一个数组 里面为对象
                        var li = "";
                        //jq中遍历数组
                        $(data).each(function(k,v){
                            li += "<li>";
                            //如果这个类型是可选的就有个[+]
                            if(v.attr_type == 1)
                            {   
                                //设置点击事件 this为a节点
                                li += "<a onclick='addAttr(this);' href='#'>[+]</a>";
                            }
                            //属性名称
                            li += v.attr_name + ':';
                            //如果属性有可选值做下拉框 否则做文本框
                            if(v.attr_option_values == "")
                            {
                                li += '<input name = "attr_value['+ v.id +'][]" type="text">';
                            }else
                            {
                                //name起名 将属性的id与属性值都传过去
                                li += '<select name = "attr_value['+ v.id +'][]" class="form-control"><option value="">请选择</option>';
                                //js将字符串转换成数组
                                var attr = v.attr_option_values.split(',');
                                for(var i=0; i < attr.length; i++)
                                {
                                    li += '<option value="'+attr[i] + '">';
                                    li +=  attr[i];
                                    li += '</option>'
                                }
                                li += '</select>';
                            }
                            li += '</li>';
                           
                        });
                     $('.ul').html(li);   
                    },
                    error:function()
                    {
                        alert('没有属性');
                    }
                });
            }else
            {
                $('.ul').html();
            }   
        });

    }
function addAttr(b)
{
    //获取li  $(节点对象)  将js节点对象转换成jq对象
    var li = $(b).parent();
    if($(b).text() == '[+]')
    {
        var newLi = li.clone();
        //去除新加的值的选中状态
        newLi.find('option:selected').removeAttr('selected');
        //把克隆迟来的隐藏域里的ID清空
        newLi.find("input[name='goods_attr_id[]']").val('');
        //+变-
        newLi.find("a").text('[-]');
        $(li).after(newLi);
    }else
    {
        var gid = li.find("input[name='goods_attr_id[]']").val();

        if(gid == '')
        {
            li.remove();
        }else
        {
            if(confirm('确定删除吗?删除后库存量也会被删除'))
            {
                $.ajax({
                    type:'POST',
                    url:"{{ url('admin/goods/ajaxDelAttr') }}/{{ $data -> id }}",
                    data:{id:gid},
                    success:function(data)
                    {
                        li.remove();
                    },
                    error:function()
                    {
                        alert('错误');
                    }

                });
            }
        }

    }

}

function addKuozhan()
{  
    var newA = $('#goods_name').before($('#kuozhan').clone());
}

 </script>
@endsection